/* File: MvbView.java
www.cs.ubc.ca/~ramesh/cpsc304/tutorial/JDBC/Swing/jdbc_swing.html

 */

package ORAjdbcSwing;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


/*
 * MvbView allows a user to view and manipulate the branch table.
 * Additional functionality may be added in the future, such as
 * viewing and manipulating the driver, license, and exam tables.
 */
public class MvbView extends JFrame
{
    // initial position of the main frame
    private int framePositionX;
    private int framePositionY;

    // initial size of main frame
    private Rectangle frameBounds = null;

    // the status text area for displaying error messages
    private JTextArea statusField = new JTextArea(5,0);

    // the scrollpane that will hold the table of database data
    private JScrollPane tableScrPane = new JScrollPane();

    // the branch admin menu
    private JMenu usersAdmin;
    private JMenu HardwareAdmin;
    private JMenu User_HardwareAdmin;
    /*
     * Default constructor. Constructs the main window.
     */
    public MvbView()
    {
        // should call the constructor of the superclass first
        super(" JL SOFT @ Monte Banato ");
        setSize(650,450);
        this.updateStatusBar("http://code.google.com/p/javajdbc/");
        this.updateStatusBar("http://code.google.com/p/jtelmon/");
        // the content pane;
        // components will be spaced vertically 10 pixels apart
        JPanel contentPane = new JPanel(new BorderLayout(0, 10));
        setContentPane(contentPane);

        // leave some space around the content pane
        contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // setup the menubar
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        // indent first menu
        menuBar.add(Box.createRigidArea(new Dimension(10,0)));

        // sets up the branch administration menu and adds it to the
        // menu bar
        setupUsersAdminMenu(menuBar);
        setupHardwareAdminMenu(menuBar);
        setupUser_HardwareAdminMenu(menuBar);
        // Setup some other menus. You may be asked to add functionality
        // to these menus in an assignment.
    //    JMenu menu = new JMenu("Hardware Admin");
      //  menu.setMnemonic(KeyEvent.VK_D);
        //menuBar.add(menu);
       // JMenu menu = new JMenu("License Admin");
        //menu.setMnemonic(KeyEvent.VK_L);
        //menuBar.add(menu);
        //menu = new JMenu("Exam Admin");
        //menu.setMnemonic(KeyEvent.VK_E);
        //menuBar.add(menu);

        // the scrollpane for the status text field
        JScrollPane statusScrPane = new JScrollPane(statusField);
        statusScrPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        statusScrPane.setBorder(BorderFactory.createLoweredBevelBorder());

        // set status field properties
        statusField.setEditable(false);
        statusField.setLineWrap(true);
        statusField.setWrapStyleWord(true);

        // add the panes to the content pane
        contentPane.add(tableScrPane, BorderLayout.CENTER);
        contentPane.add(statusScrPane, BorderLayout.NORTH);

        // center the main window
        Dimension screenSize = getToolkit().getScreenSize();
        frameBounds = getBounds();
        framePositionX = (screenSize.width - frameBounds.width)/2;
        framePositionY = (screenSize.height - frameBounds.height)/2;
        setLocation(framePositionX, framePositionY);

        // anonymous inner class to terminate program
        addWindowListener(new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                        System.exit(0);
                }
            });
    }


    /*
     * Adds menu items to the Branch Admin menu and then
     * adds the menu to the menubar
     */
    private void setupUsersAdminMenu(JMenuBar mb)
    {
        usersAdmin = new JMenu("Users Admin");

        // when alt-b is pressed on the keyboard, the menu will appear
        usersAdmin.setMnemonic(KeyEvent.VK_B);

        createMenuItem(usersAdmin, "Insert Users...",
                       KeyEvent.VK_I, "Insert Users");

        createMenuItem(usersAdmin, "Update Users Name...",
                       KeyEvent.VK_U, "Update Users");

        createMenuItem(usersAdmin, "Delete Users...",
                       KeyEvent.VK_D, "Delete Users");

        JMenuItem menuItem = createMenuItem(usersAdmin, "Show All Users",
                                            KeyEvent.VK_S, "Show Users");
        // setup a short cut key for this menu item
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, ActionEvent.CTRL_MASK));

        createMenuItem(usersAdmin, "Edit All Users",
                       KeyEvent.VK_E, "Edit Users");

        mb.add(usersAdmin);
    }





private void setupHardwareAdminMenu(JMenuBar mb)
{
  HardwareAdmin = new JMenu("Hardware Admin");
  HardwareAdmin.setMnemonic(KeyEvent.VK_D);

  createMenuItem(HardwareAdmin, "Insert Hardware...",
                 KeyEvent.VK_I, "Insert Hardware");
  createMenuItem(HardwareAdmin, "Update Hardware...",
          KeyEvent.VK_U, "Update Hardware");
  createMenuItem(HardwareAdmin, "Delete Hardware...",
          KeyEvent.VK_U, "Delete Hardware");

  JMenuItem menuItem = createMenuItem(HardwareAdmin, "Show All Hardware",
                                      KeyEvent.VK_S, "Show Hardware");

  menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,
                              ActionEvent.CTRL_MASK));
   createMenuItem(HardwareAdmin, "Edit All Hardware",
                       KeyEvent.VK_E, "Edit Hardware");

  mb.add(HardwareAdmin);
}

private void setupUser_HardwareAdminMenu(JMenuBar mb)
{
  User_HardwareAdmin = new JMenu("User Hardware");
  User_HardwareAdmin.setMnemonic(KeyEvent.VK_L);

  createMenuItem(User_HardwareAdmin, "Insert Hardware...",
                 KeyEvent.VK_I, "Insert Hardware");
  createMenuItem(User_HardwareAdmin, "Update Hardware...",
          KeyEvent.VK_U, "Update Hardware");
  createMenuItem(User_HardwareAdmin, "Delete Hardware...",
          KeyEvent.VK_U, "Delete Hardware");

  JMenuItem menuItem = createMenuItem(User_HardwareAdmin, "Show All Hardware",
                                      KeyEvent.VK_S, "Show Hardware");

  menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,
                              ActionEvent.CTRL_MASK));
   createMenuItem(User_HardwareAdmin, "Edit All Hardware",
                       KeyEvent.VK_E, "Edit Hardware");

  mb.add(User_HardwareAdmin);
}



    /*
     * Creates a menu item and adds it to the given menu.  If the menu item
     * has no mnemonic, set mnemonicKey to a negative integer. If it has no
     * action command, set actionCommand to the empty string "". By setting
     * the menu item's action command, the event handler can determine which
     * menu item was selected by the user. This method returns the menu item.
     */
    private JMenuItem createMenuItem(JMenu menu, String label, int mnemonicKey, String actionCommand)
    {
        JMenuItem menuItem = new JMenuItem(label);

        if (mnemonicKey > 0)
        {
            menuItem.setMnemonic(mnemonicKey);
        }

        if (actionCommand.length() > 0)
        {
            menuItem.setActionCommand(actionCommand);
        }

        menu.add(menuItem);

        return menuItem;
    }


    /*
     * Places the given window approximately at the center of the screen
     */
    public void centerWindow(Window w)
    {
        Rectangle winBounds = w.getBounds();
        w.setLocation(framePositionX + (frameBounds.width - winBounds.width)/2,
                      framePositionY + (frameBounds.height - winBounds.height)/2);
    }


    /*
     * This method adds the given string to the status text area
     */
    public void updateStatusBar(String s)
    {
        // trim() removes whitespace and control characters at both ends of the string
        statusField.append(s.trim() + "\n");

        // This informs the scroll pane to update itself and its scroll bars.
        // The scroll pane does not always automatically scroll to the message that was
        // just added to the text area. This line guarantees that it does.
        statusField.revalidate();
    }


    /*
     * This method adds the given JTable into tableScrPane
     */
    public void addTable(JTable data)
    {
        tableScrPane.setViewportView(data);
    }



    /*
     * This method registers the controllers for all items in each menu. This
     * method should only be executed once.
     */
    public void registerControllers()
    {
        JMenuItem menuItem;

        // UsersController handles events on the branch admin menu items (i.e. when they are clicked)


        UsersController bc = new UsersController(this);
        for (int i = 0; i < usersAdmin.getItemCount(); i++)
        {
            menuItem = usersAdmin.getItem(i);
            menuItem.addActionListener(bc);
        }
         HardwareController hc = new HardwareController(this);

        for (int i = 0; i < HardwareAdmin.getItemCount(); i++)
       {
            menuItem = HardwareAdmin.getItem(i);
           menuItem.addActionListener(hc);
        }
         User_HardwareController uc = new User_HardwareController(this);
         for (int i = 0; i < User_HardwareAdmin.getItemCount(); i++)
         {
             menuItem = User_HardwareAdmin.getItem(i);
             menuItem.addActionListener(uc);
         }
    }
    


// {
       // JMenuItem menuItem;

        // UsersController handles events on the branch admin menu items (i.e. when they are clicked)
        
            
    ///    HardwareController bc = new HardwareController(this);

       // for (int i = 0; i < HardwareAdmin.getItemCount(); i++)
     //   {
     //       menuItem = HardwareAdmin.getItem(i);
    //        menuItem.addActionListener(bc);
     //   }
  //  }
    




    public static void main(String[] args)
    {
       try {
            // =======================================================================================
                   UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
            // =======================================================================================
            // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception e) {
            e.printStackTrace();
        }

        MvbView mvb = new MvbView();

        // we will not call pack() on the main frame
        // because the size set by setSize() will be ignored
        mvb.setVisible(true);

        // create the login window
        LoginWindow lw = new LoginWindow(mvb);

        lw.addWindowListener(new ControllerRegister(mvb));

        // pack() has to be called before centerWindow()
        // and setVisible()
        lw.pack();

        mvb.centerWindow(lw);

        lw.setVisible(true);
    }
}


/*
 * Event handler for login window. After the user logs in (after login
 * window closes), the controllers that handle events on the menu items
 * are created. The controllers cannot be created before the user logs
 * in because the database connection is not valid at that time. The
 * models that are created by the controllers require a valid database
 * connection.
 */
class ControllerRegister extends WindowAdapter
{
    private MvbView mvb;

    public ControllerRegister(MvbView mvb)
    {
        this.mvb = mvb;
    }

    public void windowClosed(WindowEvent e)
    {
        mvb.registerControllers();
    }
}

